<?php
class Solr_Util_StringUtils {
    public static function merge_paymethod_to_string ($pm1,$pm2) {
        return $pm1 . '|' . $pm2;
    }
    public static function pay_method_to_array($str) {
        return explode("|",$str);
    }
    public static function convert_address_to_string ($address) {
        if(preg_match("/(.*),(.*),(.*)/i",$address)) {
            $arr = explode(",",$address);
            return $arr[0];
        } else {
            $addArray = explode("|",$address);
            $addStr = "";
            foreach($addArray as $key => $value)
            {
                if($key == 0 && $value)$addStr = $addStr . $value;
                if($key == 1 && $value)$addStr = $addStr . $value;
            }
            return $addStr;
        }
    }
    public static function merge_address_to_string ($addr1,$addr2,$addr3) {
        $addr = str_replace(",","，",$addr1).",".str_replace(",","，",$addr2).",".str_replace(",","，",$addr3);
        return $addr;
    }
    public static function property_address_to_array ($addr) {
        if(preg_match('/(.*),(.*),(.*)/i',$addr)) {
            return explode(",",$addr);
        } else {
            return explode("|",$addr);
        }
    }
    public static function truncate($str,$lenth,$dot = "..."){
        $strlen = strlen($str);
        $charlen = 0;
        $cut = false;
        for($i=0;$i<$strlen;)
        {
            $charAt = ord($str[$i]);
            if(($charAt & 0xfe) == 0xfe)//占七个字节的汉字1111 1110
            {
                $i+=7;
                $charlen+=2;
            }
            else if(($charAt & 0xfc) == 0xfc)//占六个字节的汉字1111 1100
            {
                $i+=6;
                $charlen+=2;
            }
            else if(($charAt & 0xf8) == 0xf8)//占五个字节的汉字1111 1000
            {
                $i+=5;
                $charlen+=2;
            }
            else if(($charAt & 0xf0) == 0xf0)//占四个字节的汉字1111 0000
            {
                $i+=4;
                $charlen+=2;
            }
            else if(($charAt & 0xe0) == 0xe0)//占三个字节的汉字1110 0000
            {
                $i+=3;
                $charlen+=2;
            }
            else if(($charAt & 0xc0) == 0xc0)//占两个字节的汉字1100 0000
            {
                $i+=2;
                $charlen+=2;
            }
            else if(($charAt & 0x08) == 0x08)//
            {
                $i++;
                continue;
                //$charlen+=2;
            }
            else//($charAt < 128)//普通字符
            {
                $i++;
                $charlen ++;
                //echo $i." ".decbin($charAt)." | ";
            }

            if(($charlen+2) > $lenth*2)
            {
                $cut = true;
                break;
            }
            //echo "$i,";
        }
        if($cut)
        {
            return substr($str,0,$i).$dot;
        }
        else
        {
            return $str;
        }
    }
    public static function string_lenth ($str) {
        $strlen = strlen($str);
        $charlen = 0;
        for($i=0;$i<$strlen;)
        {
            $charAt = ord($str[$i]);
            if(($charAt & 0xfe) == 0xfe)//占七个字节的汉字1111 1110
            {
                $i+=7;
                $charlen+=2;
            }
            else if(($charAt & 0xfc) == 0xfc)//占六个字节的汉字1111 1100
            {
                $i+=6;
                $charlen+=2;
            }
            else if(($charAt & 0xf8) == 0xf8)//占五个字节的汉字1111 1000
            {
                $i+=5;
                $charlen+=2;
            }
            else if(($charAt & 0xf0) == 0xf0)//占四个字节的汉字1111 0000
            {
                $i+=4;
                $charlen+=2;
            }
            else if(($charAt & 0xe0) == 0xe0)//占三个字节的汉字1110 0000
            {
                $i+=3;
                $charlen+=2;
            }
            else if(($charAt & 0xc0) == 0xc0)//占两个字节的汉字1100 0000
            {
                $i+=2;
                $charlen+=2;
            }
            else if(($charAt & 0x08) == 0x08)//
            {
                $i++;
                continue;
                //$charlen+=2;
            }
            else//($charAt < 128)//普通字符
            {
                $i++;
                $charlen ++;
            }
        }
        return floor($charlen/2);
    }
    public static function decode_seo_parameters($string) {
        $parameters = array();

        if (!$string) {
            return $parameters;
        }

        $list = preg_split('/-/', $string, -1);
        // the first element always empty?
        array_shift($list);
        while (!empty($list)) {
            $name = array_shift($list);
            $value = array_shift($list);
            $name = urldecode($name);
            $value = urldecode($value);

            $parameters[$name] = $value;
        }

        return $parameters;
    }

    public static function encode_seo_parameters($parameters) {
        $string = '';
        foreach($parameters as $name=>$value) {
            $string .= "-" . urlencode($name) . "-" . urlencode($value);
        }
        return $string;
    }

    public static function replace_keywords ($string) {
        $kws = array(
            "最","国家级","世界级"
        );
        $r_kws = array(
            "☆","☆☆☆","☆☆☆"
        );
        return str_replace($kws,$r_kws,$string);
    }
}
?>